#include <stdio.h>
#include <string.h>

// strncpy : copy num个字符从源字符串到目标空间。
// 如果源字符串的长度小于num，则拷贝完源字符串之后，在目标的后面追加0，直到num个

// strcpy(char *dest, const char *src)
// 源字符串必须以'\0'结束
// 会将源字符串中的'\0'copy到目标空间
// 目标空间必须可变

char *my_strcpy(char *dest, const char *src){
    do {
        *dest = *src;
        dest ++;
        src ++;
    } while(*src != '\0');
    *dest = *src;
    return dest;
}

char *my_strcpy_best(char *dest, const char *src)
{
    while ((*dest++ = *src++))
        ;
    return dest;
}

char *my_strncpy_best(char *dest, const char *src, int count)
{
    char *start = dest;
    while (count && (*dest++ = *src++)){
        count --;
    }
    if(count){
        while(--count){ // 如果有多的count，补 \0
            *dest++ = '\0';
        }
    }
    return dest;
}
int main() 
{
    char arr1[] = "hello";
    char arr2[] = "zrh";
    strcpy(arr1,arr2);
    printf("arr1 = %s \n",arr1);   // 输出zrh

    char arrEx1[] = "hello";
    char arrEx2[] = "zrh";
    my_strcpy(arrEx1, arrEx2);
    printf("arrEx1 = %s \n", arrEx1); // 输出zrh

    char a[] = "asdfg"; 
    char b[] = "qqqq";  
    strncpy(a,b,2);         // 必须是字符串才能cpy
    printf("a = %s \n", a);
    printf("b = %s \n", b);

    char c[] = "asdfg";
    const char d[] = "qqqq";
    my_strcpy_best(c,d);
    printf("c = %s \n", c);
    printf("d = %s \n", d);

    char e[] = "asdfg";
    const char f[] = "qqqq";
    my_strncpy_best(e, f, 3);
    printf("e = %s \n", e); // 输出 qqqfg
    printf("f = %s \n", f);
    
    return 0; // 返回 0
}
